package com.example.teaculture.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.teaculture.DB.AdviceSQL;
import com.example.teaculture.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class AdviceActivity extends Activity {
    private EditText advice_content;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    JSONObject responseObject = null;
    String msg = "";
    String code = "";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advice);
        AdviceSQL.initTable(AdviceActivity.this);
        //获取按钮
        Button but_help_feedback = (Button) findViewById(R.id.but_help_feedback);
        advice_content = (EditText) findViewById(R.id.help_feedback);

        Handler handler = new Handler(message -> {
            if (message.what == 1) {
                Toast.makeText(AdviceActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
            return true;
        });

        //添加点击事件 ，保存文本信息，并生成提示
        but_help_feedback.setOnClickListener(new Button.OnClickListener() {

            private String jsonData;
            private SharedPreferences preferences;

            public void onClick(View v) {
                String content = advice_content.getText().toString();
                JSONObject jsonObject = new JSONObject();
                preferences = getSharedPreferences("username", AdviceActivity.MODE_PRIVATE);
                String username = preferences.getString("username", "nothing");
                try {
                    jsonObject.put("username", username);
                    jsonObject.put("advice", content);
                    jsonObject.put("date", sdf.format(new Date()));
                    jsonData = jsonObject.toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Log.e("TAG", "onClick: " + jsonData);
                String url = "http://192.168.123.53:8080/tea/advice";
                OkHttpClient client = new OkHttpClient();
                MediaType mediaType = MediaType.parse("application/json;charset=utf-8");
                RequestBody requestBody = RequestBody.create(mediaType, jsonData);
                Request request = new Request.Builder()
                        .url(url)
                        .post(requestBody)
                        .build();
                new Thread(() -> {
                    Response response = null;
                    try {
                        response = client.newCall(request).execute();
                        String responseData = response.body().string();
                        System.out.println(responseData);

                        try {
                            responseObject = new JSONObject(responseData);
                            msg = responseObject.getString("msg");
                            code = responseObject.getString("code");
                            if(code.equals("200")){
                                Message message = new Message();
                                message.what = 1;
                                handler.sendMessage(message);
                            }
                            AdviceActivity.this.finish();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();

            }
        });

    }

    public void aback(View view) {
        this.finish();
    }
}
